package com.itheima.d5_synchronized_lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account {
    private double money;
    private String CardId;
    private final Lock lk = new ReentrantLock();

    public Account() {
    }

    public Account(String cardId, double money) {
        this.money = money;
        CardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getCardId() {
        return CardId;
    }

    public void setCardId(String cardId) {
        CardId = cardId;
    }

    public  void drawMoney(double money) {
        //先搞清楚谁取钱

        String name= Thread.currentThread().getName();
        //1.判断余额是否足够
        lk.lock();
            if(this.money >= money){
                System.out.println(name +"来取钱"+money + "成功");
                this.money -= money;
                System.out.println(name+"来取钱，余额剩余" + this.money);
            }else{
                System.out.println(name+"来取钱，余额不够");
        }
            lk.unlock();
    }
}
